package com.nj.api.service;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.nj.api.entity.Recharge;
import com.nj.api.mapper.AccountMapper;
import com.nj.api.mapper.RechargeMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;

@Service
public class PayServiceImpl implements PayService {
    @Resource
    private RechargeMapper rechargeMapper;
    @Resource
    private AccountMapper accountMapper;
    @Override
    public void payResult(String orderId, Integer status, String payType, Integer uid, BigDecimal money) {
        //无论支付成功与否都要改变充值状态，1为成功,2为失败。支付成功后改变用户账户
        Recharge recharge = new Recharge();
        recharge.setRechargeStatus(status).setChannel(payType);
        LambdaUpdateWrapper<Recharge> wrapper = new LambdaUpdateWrapper<Recharge>().eq(Recharge::getRechargeNo, orderId);
        rechargeMapper.update(recharge,wrapper);
        if (status==1){
//            Account account = new Account();
//            account.setAvailableMoney(account.getAvailableMoney().add(money));
//            LambdaQueryWrapper<Account> wrapper1 = new LambdaQueryWrapper<Account>().eq(Account::getUid,uid);
            accountMapper.updateA(uid,money);
        }
    }
}
